package com.myzl.stream.util.hc;

import com.myzl.common.constant.CacheConstants;
import com.myzl.common.core.redis.RedisCache;
import com.myzl.common.utils.StringUtils;
import com.myzl.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 * @date  2022年12月30日10:22:37
 */
@Slf4j
public class CameraCache {

    public static Integer getUserId(Integer deviceId){
        Integer userId = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(deviceId));
        if (StringUtils.isNull(userId)){
            userId = -1;
        }
        return userId;
    }

    public static void setUserId(Integer deviceId,Integer userId){
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(deviceId),userId);
    }


    public static void deleteUser(Integer deviceId){
        SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(deviceId));
    }



    public static boolean exists(Integer deviceId){
        Integer userId = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(deviceId));
        if (StringUtils.isNull(userId)) {
            return false;
        }

        return true;
    }

    /**
     * 设置cache key
     *
     * @param deviceId 参数键
     * @return 缓存键key
     */
    private static String getCacheKey(Integer deviceId)
    {
        return CacheConstants.CAMERA_LOGIN_KEY + deviceId;
    }
}
